package com.elong.android.specialhouse.activity;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.elong.android.specialhouse.ApartmentAPI;
import com.elong.android.specialhouse.account.Account;
import com.elong.android.specialhouse.entity.response.LoginRegisterResp;
import com.elong.android.specialhouse.request.PluginLoginReq;
import com.elong.framework.net.error.NetFrameworkError;
import com.elong.framework.netmid.ElongRequest;
import com.elong.framework.netmid.RequestExecutor;
import com.elong.framework.netmid.api.IHusky;
import com.elong.framework.netmid.request.RequestOption;
import com.elong.framework.netmid.response.BaseResponse;
import com.elong.framework.netmid.response.IResponse;
import com.elong.framework.netmid.response.IResponseCallback;
import com.elong.framework.netmid.response.StringResponse;

/**
 * @author kaiqi.li
 * @Description
 * @date 2015/12/14 16:35
 */
public class YouFangLoginManager implements IResponseCallback {
    public static final String TAG = YouFangLoginManager.class.getSimpleName() ;
    // 标识请求是否正在进行
    public static boolean isLogining = false ;


    public void YouFangLogin(long cardNum) {
        if (isLogining) {
            return;
        }
        PluginLoginReq req = new PluginLoginReq();
        req.ElongCard = cardNum;
        requestHttp(req, ApartmentAPI.pluginLogin, StringResponse.class);
        Log.i(TAG, "requst login ");

    }

    public ElongRequest requestHttp(RequestOption option, IHusky husky, Class<? extends IResponse<?>> beanClass) {
        if (beanClass != null) {
            option.setBeanClass(beanClass);
        } else {
            option.setBeanClass(BaseResponse.class);
        }
        if (husky != null) {
            option.setHusky(husky);
        }
        ElongRequest request = null;
        try {
            RequestOption requestOption = option.process();
            request = RequestExecutor.executeRequest(requestOption, this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return request ;
    }

    @Override
    public void onTaskReady(ElongRequest elongRequest) {
        isLogining = true ;
    }

    @Override
    public void onTaskDoing(ElongRequest elongRequest) {
        isLogining = true ;
    }

    @Override
    public void onTaskPost(ElongRequest elongRequest, IResponse<?> iResponse) {
        isLogining = false ;

        Log.i(TAG, "login response " + ((StringResponse) iResponse).getContent());

        ApartmentAPI action = (ApartmentAPI) elongRequest.getRequestOption().getHusky();
        JSONObject result = null;
        try {
            result = JSON.parseObject(((StringResponse) iResponse).getContent());
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }
        if (result == null) {
            return;
        }
        switch (action) {
            case pluginLogin:
                LoginRegisterResp response = JSONObject.parseObject(result.toJSONString(), LoginRegisterResp.class);
                if (response != null) {
                    saveAccountInfo(response, true);
                }
                break;
        }
    }

    /**
     * 保存账号信息
     */
    private void saveAccountInfo(LoginRegisterResp resp, boolean isLogin) {
        Account.getInstance().setLogin(isLogin);
        Account.getInstance().refreshSessionToken(resp.AccessToken);
        Account.getInstance().setUserId(resp.Uid);
        Account.getInstance().setAvatar(resp.Avatar);
        Account.getInstance().setIdentityPass(resp.IdentityPass);
        Account.getInstance().setIdentityPassDecription(resp.IdentityPassDecription);
        Account.getInstance().setNickName(resp.NickName);
        Account.getInstance().setIsCustomer(resp.IsCustomer == 1);
        Account.getInstance().setIsLandlord(resp.IsLandlord == 1);
        savePhoneAndPWD();
    }

    /**
     * 保存手机号和密码
     */
    private void savePhoneAndPWD() {
//        String aesPhoneNumber;
//        aesPhoneNumber = AESUtils.encryptAndEncoding(mPhoneNumber);
//        Account.getInstance().setPhoneNumber(aesPhoneNumber);
//        // 密码在内部进行了加密,只有在普通登录模式下才保存密码
//        if (isCommonLogin) {
//            Account.getInstance().setPassword(mValidateValue);
//        }
    }

    @Override
    public void onTaskCancel(ElongRequest elongRequest) {
        isLogining = false ;
    }

    @Override
    public void onTaskTimeoutMessage(ElongRequest elongRequest) {
        isLogining = false ;
    }

    @Override
    public void onTaskError(ElongRequest elongRequest, NetFrameworkError netFrameworkError) {
        isLogining = false ;
    }
}
